package it.cums.util;

import it.cums.controller.ApplicationController;
import it.cums.editors.SupplierEditorInput;
import it.cums.model.Supplier;
import it.cums.model.Suppliers;

import java.util.List;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PlatformUI;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class SupplierManager {
	private static SupplierManager supplierManager;
	
	private SupplierManager() {
	}
	
	public static SupplierManager getInstance() {
		if (supplierManager == null) {
			supplierManager = new SupplierManager();
		}
		return supplierManager;
	}
	
	public void createAndStoreSupplier(Suppliers suppliers, String name, String referencePerson, String taxNumber, String address, String cap, String location, String country, String phone, String mobilePhone, String email, String webSite, String accountDetails, String notes) throws Exception {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			Supplier s = new Supplier();
			s.setName(name);
			s.setReferencePerson(referencePerson);
			s.setTaxNumber(taxNumber);
			s.setAddress(address);
			s.setCap(cap);
			s.setLocation(location);
			s.setCountry(ApplicationController.getInstance().getCountry(country));
			s.setPhone(phone);
			s.setMobilePhone(mobilePhone);
			s.setEmail(email);
			s.setWebSite(webSite);
			s.setAccountDetails(accountDetails);
			s.setNotes(notes);
			session.save(s);
			tx.commit();
			suppliers.addSupplier(s);
		} catch (Exception e) {
			MessageCreator.showErrorMessage("Supplier '" + name + "' can't be added to the database!");
			HibernateUtil.rollback(tx);
			throw e;
		} finally {
			HibernateUtil.closeSession(session);
		}
	}
	
	public void updateSupplier(Supplier supplier, String name, String referencePerson, String taxNumber, String address, String cap, String location, String country, String phone, String mobilePhone, String email, String webSite, String accountDetails, String notes) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			supplier.setName(name);
			supplier.setReferencePerson(referencePerson);
			supplier.setTaxNumber(taxNumber);
			supplier.setAddress(address);
			supplier.setCap(cap);
			supplier.setLocation(location);
			supplier.setCountry(ApplicationController.getInstance().getCountry(country));
			supplier.setPhone(phone);
			supplier.setMobilePhone(mobilePhone);
			supplier.setEmail(email);
			supplier.setWebSite(webSite);
			supplier.setAccountDetails(accountDetails);
			supplier.setNotes(notes);
			session.update(supplier);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Supplier> listSuppliers() {
		Session session = null;
		Transaction tx = null;
		List<Supplier> suppliers = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
		    tx = session.beginTransaction();
		    suppliers = session.createQuery("from Supplier").list();
		    tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
		return suppliers;
    }
	
	public void deleteSupplier(Suppliers suppliers, Supplier supplier) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			session.delete(supplier);
			tx.commit();
			suppliers.removeSupplier(supplier);
			closeEditors(supplier);
		} catch (Exception e) {
			MessageCreator.showErrorMessage("Supplier '" + supplier.getName() + "' can't be removed from the database!");
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
	}
	
	private void closeEditors(Supplier supplier) {
		try {
			for (IEditorReference reference : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
				IEditorPart editorPart = reference.getEditor(false);
				IEditorInput input = reference.getEditorInput();
				if (input instanceof SupplierEditorInput) {
					SupplierEditorInput supplierInput = (SupplierEditorInput)input;
					if (supplierInput.getSupplier().getId() == supplier.getId()) {
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(editorPart, false);
					}
				}
			}
		} catch (Exception e) {
			//ignore exception
		}
	}
}